<template>
  <div class="netdisk-header">
    <div v-show="netKing != 3">
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        v-if="netFileSelectFlag"
        :disabled="netCheckedFile.length == 0"
        @click="handleSelect"
        >确定</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-close"
        plain
        v-if="netFileSelectFlag"
        @click="handleDeselect"
        >取消</el-button
      >
      <el-divider v-if="netFileSelectFlag" direction="vertical"></el-divider>

      <el-button
        type="primary"
        size="small"
        icon="el-icon-upload2"
        @click="uploadFiles"
        :disabled="netKing != 1"
        >上传文件</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-folder-add"
        plain
        @click="newFolder"
        v-if="!netFileSelectFlag && !netShowType"
        :disabled="netKing != 1"
        >新建文件夹{{ netShowType }}</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-rank"
        plain
        v-if="!netFileSelectFlag"
        :disabled="netKing != 1 || netCheckedFile.length == 0"
        @click="handleMove"
        >移动</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-delete"
        plain
        v-if="!netFileSelectFlag"
        :disabled="netKing != 1 || netCheckedFile.length == 0"
        @click="deleteFiles"
        >删除</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-download"
        plain
        v-if="!netFileSelectFlag"
        :disabled="netCheckedFile.length == 0"
        @click="downloadFiles"
        >下载</el-button
      >
    </div>
    <!-- 回收站按钮 -->
    <div v-show="netKing == 3">
      <el-button
        type="primary"
        size="small"
        icon="el-icon-delete-solid"
        @click="handleClear"
        >清空</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-refresh-left"
        @click="handleRecovery"
        plain
        :disabled="netCheckedFile.length == 0"
        >恢复</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-circle-close"
        @click="handleDelete"
        plain
        :disabled="netCheckedFile.length == 0"
        >彻底删除</el-button
      >
    </div>
    <div class="search">
      <el-input
        placeholder="请输入内容"
        v-model="listQuery.oldName"
        clearable
        size="small"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="handleSearch"
        ></el-button>
      </el-input>
    </div>
    <CreateFolder />
    <UploadFile />
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  components: {
    CreateFolder: () => import('../header/CreateFolder.vue'),
    UploadFile: () => import('../header/UploadFile.vue'),
  },
  data() {
    return {
      listQuery: {
        oldName: '',
      },
    }
  },
  computed: mapGetters([
    'netCheckedFile',
    'netKing',
    'netShowType',
    'netFileSelectFlag',
  ]),
  mounted() {},
  methods: {
    newFolder() {
      this.$bus.$emit('createFolder')
    },
    uploadFiles() {
      this.$bus.$emit('uploadFile')
    },
    async deleteFiles() {
      this.deleteTips(async () => {
        let list = this.netCheckedFile.map((v) => v.id)
        await this.$store.dispatch('batchBanTbNetDisk', list)
        this.$bus.$emit('refreshTbNetDiskList')
      })
    },
    downloadFiles() {
      let files = this.netCheckedFile.filter((v) => v.isFolder == 2)
      if (files.length == 0) {
        this.warnMsg('请选择要下载的文件')
        return
      }
      this.$bus.$emit('downloadZip', files)
    },
    //移动
    handleMove() {
      this.$bus.$emit('moveFile')
    },
    //恢复
    async handleRecovery() {
      this.deleteTips(
        async () => {
          let list = this.netCheckedFile.map((v) => v.id)
          await this.$store.dispatch('batchRecoveryTbNetDisk', list)
          this.$bus.$emit('refreshTbNetDiskList')
        },
        '确定要恢复吗?',
        '恢复',
      )
    },
    //彻底删除
    handleDelete() {
      this.deleteTips(
        async () => {
          let list = this.netCheckedFile.map((v) => v.id)
          await this.$store.dispatch('batchDeleteTbNetDisk', list)
          this.$bus.$emit('refreshTbNetDiskList')
        },
        '确定要彻底删除吗?',
        '删除',
      )
    },
    //清空回收站
    handleClear() {
      this.deleteTips(
        async () => {
          await this.$store.dispatch('clearTbNetDisk')
          this.$bus.$emit('refreshTbNetDiskList')
        },
        '确定要清空回收站吗?',
        '清空',
      )
    },
    handleSelect() {
      this.$bus.$emit('closeSelectFileDialog', this.netCheckedFile)
    },
    handleDeselect() {
      this.$bus.$emit('closeSelectFileDialog')
    },
    //搜索
    handleSearch() {
      this.$bus.$emit('refreshTbNetDiskList', this.listQuery)
    },
  },
}
</script>
<style scoped lang="scss">
.netdisk-header {
  position: relative;
  .search {
    position: absolute;
    right: 0;
    top: 0;
  }
}
</style>